package com.xinjian.jianba.filter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.io.IOUtils;

import com.xinjian.jianba.dto.SignDto;
import com.xinjian.jianba.utils.JsonUtil;
import com.xinjian.jianba.utils.QiNiuUtil;
import com.xinjian.jianba.utils.STSUtil;

public class ContentCachingRequestWrapper extends HttpServletRequestWrapper{

    private  byte[] body;

    private BufferedReader reader;

    private ServletInputStream inputStream;

    public ContentCachingRequestWrapper(HttpServletRequest request) throws IOException{
        super(request);
        String url=request.getRequestURI();
        byte[] signBody = IOUtils.toByteArray(request.getInputStream());
        if(!QiNiuUtil.backRequestUri.equals(url)&&!STSUtil.backRequestUri.equals(url)&&url.indexOf("/api/v3/jianba/tickets")==-1&&!"/api/v3/jianba/sts/videoInfo/ac/videoPath".equals(url)&&!"/api/v3/jianba/pay/notify/WEIXIN".equals(url)){
        SignDto signDto=JsonUtil.json2Obj(new String(signBody,request.getCharacterEncoding()), SignDto.class);
        if(signDto==null||signDto.getSign()==null||signDto.getSign().equals("")){
        	body="Y7mb+EJwjFBM7TCu9ejqAA==".getBytes(request.getCharacterEncoding());
        	return;
        }
        body=signDto.getSign().getBytes(request.getCharacterEncoding());
        }else{
        	body=signBody;
        }
        inputStream = new RequestCachingInputStream(body);
    }
    public byte[] getBody() {
        return body;
    }


    public void setBody(byte[] body) {
		this.body = body;
	}
	public void setReader(BufferedReader reader) {
		this.reader = reader;
	}
	public void setInputStream(ServletInputStream inputStream) {
		this.inputStream = inputStream;
	}
	@Override
    public ServletInputStream getInputStream() throws IOException {
		return new RequestCachingInputStream(body);
    }

    @Override
    public BufferedReader getReader() throws IOException {
        if (reader == null) {
            reader = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding()));
        }
        return reader;
    }

    private  class RequestCachingInputStream extends ServletInputStream {

        private final ByteArrayInputStream inputStream;

        public RequestCachingInputStream(byte[] bytes) {
            inputStream = new ByteArrayInputStream(bytes);
        }
        @Override
        public int read() throws IOException {
            return inputStream.read();
        }

        @Override
        public boolean isFinished() {
            return inputStream.available() == 0;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener readlistener) {
        }

    }

}
